package API;

/**
 * 五个异常
 */
public class hw27 {
    //
    import java.util.InputMismatchException;
import java.util.Scanner;
    //1.
    public class InputMismatchExceptionExample {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            try {
                int num = scanner.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("发生输入不匹配异常：输入不是整数。");
            }
            scanner.close();
        }
    }

    //2.
    class Base {
        int num;
    }
    class Derived extends Base {
        double num;
    }
    public class InheritanceErrorExample {
        public static void main(String[] args) {
            Derived d = new Derived();
            d.num = 10;
            System.out.println(d.num);
        }
    }

    //3.
    public class MethodCallErrorExample {
        public static void main(String[] args) {
            int result = add(5);
            System.out.println(result);
        }
    }

    //4.
    import java.io.File;
import java.io.FileNotFoundException;

    public class FileNotFoundExceptionExample {
        public static void main(String[] args) {
            try {File file = new File("nonExistentFile.txt");
            } catch (FileNotFoundException e) {
                System.out.println("发生文件不存在异常");
            }
        }
    }

    //5.
    public class ClassCastExceptionExample {
        public static void main(String[] args) {
            Object obj = new Integer(10);
            try {
                String str = (String) obj;
            } catch (ClassCastException e) {System.out.println("发生类型转换异常");
            }
        }
    }
}
